//bmi.js
const util = require('../../utils/util.js')
const years = []
for (let i = 18; i <= 100; i++) {
  years.push(i)
}

Page({
  data: {
    height: 175,
    weight: 65,
    array: years,
    index: 7,
    age: 25,
    sex: "male",
    display: "none",
    percent: 31,
    sex_img: "/images/healthy-boy.png",
    modalHidden: true,
  },
  onLoad: function () {

  },
  sliderHeightChange: function (e) {    //身高滑动器
    this.setData({
      height: e.detail.value
    })
  },
  sliderWeightChange: function (e) {    //体重滑动器
    this.setData({
      weight: e.detail.value
    })
  },
  switchSexChange: function (e){           //性别开关按钮
    var is_switch = e.detail.value
    var sex = ""
    if (is_switch){
      sex = "female"
    }
    else{
      sex = "male"
    }
    this.setData({      
      sex: sex
    })
  },
  pickerAgeChange: function (e) {       //年龄滚动蒙版
    var indexstr = parseInt(e.detail.value)
    var age = indexstr + 18    
    this.setData({
      index: e.detail.value,
      age: age
    })
  },
  showResult: function() {
    var current_height = this.data.height /100
    var current_weight = this.data.weight
    var current_age = this.data.age
    var current_sex = this.data.sex
    var BMI = current_weight / current_height / current_height
    var BMI_Index = BMI.toFixed(1)
    var BMI_Text = ""
    var BMI_Color = ""
    var Pro_Color = ""
    var tips = ""    
    if (BMI_Index < 18.5) {
      BMI_Text = "体重过轻"
      tips = "你的体重属于偏瘦范围，建议加强营养，多摄取高蛋白低纤维食物。"
      BMI_Color = "#BC8F8F"
      Pro_Color = "#BC8F8F"
    }
    if (BMI_Index >= 18.5 && BMI_Index <= 23.9) {
      BMI_Text = "标准身材"
      tips = "你的体重属于标准范围，切勿暴饮暴食或盲目减肥，继续保持完美身材。"
      BMI_Color = "#6E8B3D"
      Pro_Color = "#6E8B3D"
    }
    if (BMI_Index >= 24.0 && BMI_Index <= 27.9) {
      BMI_Text = "体重过重"
      tips = "你的体重属于超重范围，建议积极健身运动，搭配合理的饮食，有助于早日减重哟。"
      BMI_Color = "#F4A460"
      Pro_Color = "#F4A460"
    }
    if (BMI_Index >= 28.0 && BMI_Index <= 32.0) {
      BMI_Text = "I级肥胖"
      tips = "你的体重属于轻度肥胖，对体重需提起重视，合理饮食积极健身，能够帮你早日减重。"
      BMI_Color = "#FF6A6A"
      Pro_Color = "#FF6A6A"
    }
    if (BMI_Index > 32.0 && BMI_Index <= 35.0) {
      BMI_Text = "II级肥胖"
      tips = "你的体重属于中度肥胖，建议咨询医生并加强自身饮食管理，祝你早日回归完美身材。"
      BMI_Color = "#FF6A6A"
      Pro_Color = "#FF6A6A"
    }
    if (BMI_Index > 35.0 || BMI_Index < 15.0) {     
      this.noTitlemodalTap()
      this.setData({        
        display: "none",
      })
    }
    else{      
      if (current_sex == "male") {
        var BMR = 66 + (13.7 * current_weight) + (5.0 * current_height * 100)
        - (6.8 * current_age)
        var sex_img = "/images/healthy-boy.png"
      }
      if (current_sex == "female") {
        var BMR = 655 + (9.6 * current_weight) + (1.7 * current_height * 100) 
        - (4.7 * current_age)
        var sex_img = "/images/healthy-girl.png"
      }
      var BMR_Index = BMR.toFixed(1)      
      var percent = (BMI_Index - 15) * 5 
      this.setData({
        bmi: BMI_Index,
        range: BMI_Text,
        tips: tips,
        color: BMI_Color,
        bmr: BMR_Index,
        display: "block",
        percent: percent,
        Pro_Color: Pro_Color,
        sex_img: sex_img,
      })
    }    
  },
  noTitlemodalTap: function (e) {
    wx.showModal({
      showCancel: false,
      content: "你是魔鬼吧？？？",
      confirmText: "返回",
      confirmColor: "#669999",
    })
  },
  onShareAppMessage: function () { //用户点击右上角分享   
    return {
      title: '贴身管家分享', // 分享标题
      desc: '贴身管家分享', // 分享描述
      path: 'pages/healthy/healthy' // 分享路径
    }
  },
})
